# Frontend Toolkit Makefile
# 自动化常用开发任务

# 默认目标
.DEFAULT_GOAL := help

# 构建项目
build:
	@echo "🔨 构建项目..."
	cargo build --release

# 安装 git-cliff
install-git-cliff:
	@echo "📦 安装 git-cliff..."
	cargo install git-cliff

# 安装 git hooks
install-hooks:
	@echo "🔧 安装 Git Hooks..."
	@chmod +x scripts/install-hooks.sh
	@bash scripts/install-hooks.sh

# 卸载 git hooks
uninstall-hooks:
	@echo "🗑️  卸载 Git Hooks..."
	@rm -f .git/hooks/pre-commit
	@rm -f .git/hooks/commit-msg
	@rm -f .git/hooks/pre-push
	@echo "✅ Git Hooks 已卸载"

# 重新安装 git hooks
reinstall-hooks: uninstall-hooks install-hooks
	@echo "✅ Git Hooks 重新安装完成"

# 测试提交信息格式
test-commit-format:
	@echo "🧪 测试提交信息格式校验..."
	@echo "测试无效格式" > /tmp/test-invalid.txt
	@echo "测试: cargo run --bin check-commit-msg -- /tmp/test-invalid.txt"
	@cargo run --bin check-commit-msg -- /tmp/test-invalid.txt || true
	@echo ""
	@echo "feat: 测试有效格式" > /tmp/test-valid.txt
	@echo "测试: cargo run --bin check-commit-msg -- /tmp/test-valid.txt"
	@cargo run --bin check-commit-msg -- /tmp/test-valid.txt
	@rm -f /tmp/test-invalid.txt /tmp/test-valid.txt

# 交互式提交
commit:
	@echo "🚀 启动交互式提交工具..."
	@chmod +x scripts/interactive-commit.sh
	@bash scripts/interactive-commit.sh

# 快速提交 (类似 npm run commit)
commitizen:
	@echo "🚀 启动 Commitizen 风格提交工具..."
	@chmod +x scripts/interactive-commit.sh
	@bash scripts/interactive-commit.sh

# 检查代码格式
check-format:
	@echo "📏 检查代码格式..."
	cargo fmt -- --check

# 格式化代码
format:
	@echo "🎨 格式化代码..."
	cargo fmt

# 运行 Clippy 检查
clippy-check:
	@echo "🔍 运行 Clippy 检查..."
	cargo clippy -- -D warnings

# 生成 changelog
changelog:
	@echo "📋 生成 changelog..."
	git-cliff --output CHANGELOG.md

# 预览未发布的变更
changelog-preview:
	@echo "🔍 预览未发布的变更..."
	git-cliff --unreleased

# 生成到指定标签的 changelog
changelog-tag:
	@echo "🏷️  生成到指定标签的 changelog..."
	@read -p "请输入标签名: " tag; \
	git-cliff --tag $$tag --output CHANGELOG.md

# 使用项目内置的 changelog 命令
changelog-cli:
	@echo "🛠️  使用内置 changelog 命令..."
	cargo run -- changelog

# 预览使用内置命令
changelog-cli-preview:
	@echo "🔍 使用内置命令预览..."
	cargo run -- changelog --preview

# 发布新版本（包含 changelog 生成）
release:
	@echo "🚀 准备发布新版本..."
	@read -p "请输入版本号 (例如: v0.1.2): " version; \
	git-cliff --tag $$version --output CHANGELOG.md && \
	git add CHANGELOG.md && \
	git commit -m "chore(release): prepare for $$version" && \
	git tag $$version && \
	echo "✅ 版本 $$version 已准备就绪，可以推送到远程仓库"

# 推送发布
push-release:
	@echo "📡 推送发布..."
	git push origin main --tags

# 清理构建文件
clean:
	@echo "🧹 清理构建文件..."
	cargo clean

# 运行测试
test:
	@echo "🧪 运行测试..."
	cargo test

# 代码检查
lint:
	@echo "🔍 代码检查..."
	cargo clippy -- -D warnings

# 格式化代码
fmt:
	@echo "🎨 格式化代码..."
	cargo fmt

# 完整的质量检查
check: fmt lint test
	@echo "✅ 质量检查完成"

# 显示帮助信息
help:
	@echo "Frontend Toolkit 开发工具"
	@echo ""
	@echo "可用命令:"
	@echo "  build                构建项目"
	@echo "  install-git-cliff    安装 git-cliff 工具"
	@echo "  changelog            生成 changelog"
	@echo "  changelog-preview    预览未发布的变更"
	@echo "  changelog-tag        生成到指定标签的 changelog"
	@echo "  changelog-cli        使用内置 changelog 命令"
	@echo "  changelog-cli-preview 使用内置命令预览"
	@echo "  release              发布新版本（包含 changelog）"
	@echo "  push-release         推送发布"
	@echo "  clean                清理构建文件"
	@echo "  test                 运行测试"
	@echo "  lint                 代码检查"
	@echo "  fmt                  格式化代码"
	@echo "  check                完整的质量检查"
	@echo "  install-hooks        安装 Git Hooks"
	@echo "  uninstall-hooks      卸载 Git Hooks"
	@echo "  reinstall-hooks      重新安装 Git Hooks"
	@echo "  test-commit-format   测试提交信息格式校验"
	@echo "  commit               交互式提交工具"
	@echo "  commitizen           Commitizen 风格提交"
	@echo "  help                 显示此帮助信息"

.PHONY: build install-git-cliff install-hooks uninstall-hooks reinstall-hooks test-commit-format commit commitizen changelog changelog-preview changelog-tag changelog-cli changelog-cli-preview release push-release clean test lint fmt check help